Skip to content

Fix: Avoid duplicate pip package entries in results#1561

Merged
mohnjiles merged 2 commits intoLykosAI:mainfrom
e-nord:fix-pip-show-results-crash
Mar 1, 2026
Merged

Fix: Avoid duplicate pip package entries in results#1561
mohnjiles merged 2 commits intoLykosAI:mainfrom
e-nord:fix-pip-show-results-crash

Conversation

@e-nord
Copy link
Copy Markdown
Contributor

@e-nord e-nord commented Mar 1, 2026

Added new test cases to smoke out the issue that at least I was running into. There's a couple hits in existing issues given the trace:

dotnet test StabilityMatrix.Tests --filter "FullyQualifiedName~PipShowResultsTests"
...
StabilityMatrix.Tests test failed with 2 error(s) (2.0s)
    F:\projects\StabilityMatrix\StabilityMatrix.Core\Python\PipShowResult.cs(48): error TESTERROR:
      TestMultiplePackages (13ms): Error Message: Test method StabilityMatrix.Tests.Core.PipShowResultsTests.TestMultiplePackages threw exception:
      System.ArgumentException: An item with the same key has already been added. Key: Name
      Stack Trace:
          at System.Collections.Generic.Dictionary`2.TryInsert(TKey key, TValue value, InsertionBehavior behavior)
         at System.Collections.Generic.Dictionary`2.Add(TKey key, TValue value)
         at System.Linq.Enumerable.ToDictionary[TSource,TKey,TElement](IEnumerable`1 source, Func`2 keySelector, Func`2 elementSelector, IEqualityComparer`1 comparer)
         at System.Linq.Enumerable.ToDictionary[TSource,TKey,TElement](IEnumerable`1 source, Func`2 keySelector, Func`2 elementSelector)
         at StabilityMatrix.Core.Python.PipShowResult.Parse(String output) in F:\projects\StabilityMatrix\StabilityMatrix.Core\Python\PipShowResult.cs:line 48
         at StabilityMatrix.Tests.Core.PipShowResultsTests.TestMultiplePackages() in F:\projects\StabilityMatrix\StabilityMatrix.Tests\Core\PipShowResultsTests.cs:line 62
         at System.RuntimeMethodHandle.InvokeMethod(Object target, Void** arguments, Signature sig, Boolean isConstructor)
         at System.Reflection.MethodBaseInvoker.InvokeWithNoArgs(Object obj, BindingFlags invokeAttr)
    F:\projects\StabilityMatrix\StabilityMatrix.Core\Python\PipShowResult.cs(48): error TESTERROR:
      TestDuplicatePackageNameInOutput (< 1ms): Error Message: Test method StabilityMatrix.Tests.Core.PipShowResultsTests.TestDuplicatePackageNameInOutput threw exception:
      System.ArgumentException: An item with the same key has already been added. Key: Name
      Stack Trace:
          at System.Collections.Generic.Dictionary`2.TryInsert(TKey key, TValue value, InsertionBehavior behavior)
         at System.Collections.Generic.Dictionary`2.Add(TKey key, TValue value)
         at System.Linq.Enumerable.ToDictionary[TSource,TKey,TElement](IEnumerable`1 source, Func`2 keySelector, Func`2 elementSelector, IEqualityComparer`1 comparer)
         at System.Linq.Enumerable.ToDictionary[TSource,TKey,TElement](IEnumerable`1 source, Func`2 keySelector, Func`2 elementSelector)
         at StabilityMatrix.Core.Python.PipShowResult.Parse(String output) in F:\projects\StabilityMatrix\StabilityMatrix.Core\Python\PipShowResult.cs:line 48
         at StabilityMatrix.Tests.Core.PipShowResultsTests.TestDuplicatePackageNameInOutput() in F:\projects\StabilityMatrix\StabilityMatrix.Tests\Core\PipShowResultsTests.cs:line 171
         at System.RuntimeMethodHandle.InvokeMethod(Object target, Void** arguments, Signature sig, Boolean isConstructor)
         at System.Reflection.MethodBaseInvoker.InvokeWithNoArgs(Object obj, BindingFlags invokeAttr)

Test summary: total: 5, failed: 2, succeeded: 3, skipped: 0, duration: 2.0s
Build failed with 2 error(s) and 394 warning(s) in 51.1s

Passes after applying the changes.

@github-actions
Copy link
Copy Markdown

github-actions bot commented Mar 1, 2026

CLA Assistant Lite bot CLA Assistant bot All Contributors have signed the CLA.

@gemini-code-assist
Copy link
Copy Markdown
Contributor

Summary of Changes

Hello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request addresses a critical parsing issue in the PipShowResult.Parse method that previously led to an ArgumentException when processing pip show output with multiple or repeated package information. The core change involves refining the parsing logic to gracefully handle such inputs by only extracting details for the first encountered package. This ensures the application's stability and robustness when interacting with pip's output, backed by newly added, targeted unit tests.

Highlights

  • Bug Fix: Resolved an ArgumentException that occurred when parsing pip show output containing multiple or duplicate package entries.
  • Parsing Logic Improvement: Refactored the PipShowResult.Parse method to use a manual iteration with TryAdd and a check for the 'Name' key, ensuring only the first package's details are processed and preventing duplicate key errors.
  • Test Coverage: Introduced a new test file, PipShowResultsTests.cs, with comprehensive unit tests covering various pip show output scenarios, including single, multiple, malformed, multi-line license, and duplicate package entries.

🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console.

Changelog
  • StabilityMatrix.Core/Python/PipShowResult.cs
    • Modified the Parse method to replace the ToDictionary LINQ extension with a manual foreach loop for dictionary population.
    • Implemented logic within the parsing loop to use TryAdd for key-value pairs and to break early if a subsequent 'Name' key is encountered, indicating the start of a new package entry.
  • StabilityMatrix.Tests/Core/PipShowResultsTests.cs
    • Added a new test class PipShowResultsTests to house unit tests for PipShowResult parsing.
    • Included TestSinglePackage to verify correct parsing of a single package entry.
    • Added TestMultiplePackages to confirm that only the first package is parsed when multiple are present.
    • Introduced TestMalformedPackage to ensure robust handling of malformed lines in the input.
    • Added TestMultiLineLicense to validate parsing of multi-line license information.
    • Included TestDuplicatePackageNameInOutput to specifically test and confirm the fix for the ArgumentException caused by duplicate package names.
Activity
  • New test cases were added to PipShowResultsTests.cs to reproduce the ArgumentException issue.
  • The PipShowResult.Parse method was refactored to prevent the ArgumentException when encountering duplicate package names.
  • The author confirmed that the changes successfully pass the newly added tests, resolving the reported crash.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request effectively resolves a crash that occurs when parsing pip show output containing multiple package definitions. The switch from a LINQ ToDictionary call to a foreach loop that processes only the first package is a direct and correct fix for the issue. The addition of a comprehensive test suite in PipShowResultsTests.cs is excellent, as it covers various scenarios including the specific bug, ensuring future regression is prevented. I have one suggestion regarding improving the parser's robustness against invalid input, but overall this is a solid improvement.

Comment on lines +48 to +65
var linesDict = new Dictionary<string, string>();
foreach (var line in lines)
{
var split = line.Split(':', 2);
if (split.Length != 2)
continue;

var key = split[0].Trim();
var value = split[1].Trim();

if (key == "Name" && linesDict.ContainsKey("Name"))
{
// We've hit a new package, so stop parsing
break;
}

linesDict.TryAdd(key, value);
}
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

This new parsing logic correctly handles duplicate package entries. However, a potential KeyNotFoundException can still occur. If the output string is empty or contains no valid key-value pairs after filtering, linesDict will be empty. This will cause a crash on line 69 when linesDict["Name"] is accessed.

A more robust implementation would validate that linesDict contains the required 'Name' and 'Version' keys before attempting to create the PipShowResult object, throwing a more specific exception like FormatException if they are missing.

@e-nord
Copy link
Copy Markdown
Contributor Author

e-nord commented Mar 1, 2026

I have read the CLA Document and I hereby sign the CLA

Copy link
Copy Markdown
Member

@ionite34 ionite34 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good! Thanks for adding the test cases for the pip outputs too.

@mohnjiles mohnjiles merged commit 2598d69 into LykosAI:main Mar 1, 2026
2 of 3 checks passed
@github-actions github-actions bot locked and limited conversation to collaborators Mar 1, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants